package com.tutou.ikuser.config;


import com.tutou.ikuser.hander.CustomAuthenticationEntryPoint;
import com.tutou.ikuser.hander.JwtAuthenticationEntryPoint;
import com.tutou.ikuser.filter.JwtAuthenticationFilter;
import com.tutou.ikuser.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

//https://blog.csdn.net/weixin_50569789/article/details/123766739?utm_source=miniapp_weixin
//https://blog.csdn.net/J080624/article/details/131417235
//https://www.bilibili.com/video/BV1ET4y1H7KQ/?spm_id_from=333.337.search-card.all.click&vd_source=81a3e644f003cdc201c71648c281722e

@EnableWebSecurity
@Configuration
public class SecurityConfiguration {

    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;

    @Autowired
    private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;


    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                //禁用csrf
                .csrf().disable()
                //不通过session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                //配置请求授权
                .and().authorizeRequests()
                .antMatchers("/login/**").permitAll()
                //匿名访问，没认证的可以访问，认证的不可以访问
                //.antMatchers("/login/**").anonymous()
                .anyRequest().authenticated()  //其他请求需要认证

                .and().logout().permitAll()

                // 全局异常处理配置
                .and().exceptionHandling()
                .authenticationEntryPoint(customAuthenticationEntryPoint) // 处理认证异常(用户名或密码输入错误时候)
                //.authenticationEntryPoint(JwtAuthenticationEntryPoint)//处理token过期

                .and().addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    //密码加密解密
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
        return http.getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(userDetailsServiceImpl)
                .passwordEncoder(passwordEncoder())
                .and()
                .build();
    }
}
